<template>
	<view class="loginview">
		<view class="logo-box">
			<view class="logo">
				<image src="/static/logo.png" mode="widthFix"></image>
				<h1>城乡物流</h1>
			</view>
		</view>
		
		<view class="form-box">
			<u-form :model="form" ref="uForm">
				<u-form-item class="relative" label-width="50" prop="name">
					<u-icon class="icon" name="account" color="#9C9C9C" size="30"></u-icon>
					<u-input v-model="form.name" type="text" placeholder="请输入用户名" />
				</u-form-item>
				<u-form-item class="relative" label-width="50" prop="phone">
					<u-icon class="icon" name="phone" color="#9C9C9C" size="30"></u-icon>
					<u-input v-model="form.phone" type="number" placeholder="请输入手机号" />
				</u-form-item>
				<u-form-item class="relative" label-width="50" prop="idCard">
					<u-icon class="icon" name="coupon" color="#9C9C9C" size="30"></u-icon>
					<u-input v-model="form.idCard" type="text" placeholder="请输入身份证号" />
				</u-form-item>
				<u-form-item class="relative" label-width="50" prop="password">
					<u-icon class="icon" name="lock" color="#9C9C9C" size="30"></u-icon>
					<u-input v-model="form.password" type="password" placeholder="请输入密码" />
				</u-form-item>
				<u-form-item class="relative" label-width="50" prop="password2">
					<u-icon class="icon" name="lock" color="#9C9C9C" size="30"></u-icon>
					<u-input v-model="form.password2" type="password" placeholder="请输入确认密码" />
				</u-form-item>
			</u-form>
		</view>
		<view class="fiexd-btm">
			<navigator url="/pages/login/login">已有账号？马上登陆</navigator>
			<u-button class="custom-style" type="primary" @tap="submit">注册</u-button>
		</view>
	</view>
</template>

<script>
	export default {
		onReady() {
			this.$refs.uForm.setRules(this.rules)
		},
		data() {
			return {
				form: {
					name: null,
					phone: null,
					idCard: null,
					password: null,
					password2: null
				},
				rules: {
					name: [{
						required: true,
						message: '请输入姓名',
						trigger: 'change'
					}],
					phone: [{
						required: true,
						message: '请输入手机号',
						trigger: 'change'
					}, {
						// 自定义验证函数，
						validator: (rule, value, callback) => {
							return this.$u.test.mobile(value);
						},
						message: '手机号码不正确',
						trigger: 'change'
					}],
					idCard: [{
						required: true,
						message: '请输入身份证号',
						trigger: 'change'
					}, {
						// 自定义验证函数，
						validator: (rule, value, callback) => {
							return this.$u.test.idCard(value);
						},
						message: '身份证号码不正确',
						trigger: 'change'
					}],
					password: [{
						required: true,
						min: 7,
						message: '密码不能少于7个字符',
						trigger: 'change'
					}],
					password2: [{
						required: true,
						min: 7,
						message: '密码不能少于7个字符',
						trigger: 'change'
					}, {
						// 自定义验证函数，
						validator: (rule, value, callback) => {
							return this.form.password == this.form.password2;
						},
						message: '两次输入密码不一致',
						trigger: 'change'
					}]
				}
			}
		},
		methods: {
			submit(e) {
				this.$refs.uForm.validate(valid => {
					if (!valid) {
						console.log('验证失败');
						return
					} else {
						this.$api.request('POST', '/api/user/phoneReg', this.form).then(res => {
							let userInfo = res.data
							if (!res.data.token) {
								this.$api.msg('注册失败')
								return
							}
							this.$api.msg('注册成功')
							this.$store.commit('login', userInfo)
							this.$api.request('POST', '/api/user/selectUserInfo').then(res => {
								userInfo.imageUrl = res.data.imageUrl
								userInfo.nickName = res.data.nickName
								userInfo.name = res.data.name
								userInfo.id = res.data.id
								userInfo.verify = res.data.userCarOwnerVerify
								this.$store.commit('login', userInfo)
								if (userInfo.verify) {
									uni.switchTab({
										url: '/pages/index/index'
									})
								} else {
									uni.navigateTo({
										url: '/pages/car/driver?source=verify'
									})
								}
							})
						})
					}
				})
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #fff;
		height: 100%;
	}
	.logo-box {
		padding: 140upx 30upx 20upx;
		height: 520upx;
		text-align: center;
		background: url("/static/com-bg.jpg") no-repeat center center;
		background-size: 100% 100%;
		
		h1 {
			font-size: 48upx;
			font-weight: normal;
			color: #fff;
		}
		
		image {
			margin: 0 auto 20upx;
			display: block;
			width: 140upx;
			height: 140upx;
			border-radius: 140upx;
		}
	}
	
	.form-box {
		.relative {
			position: relative;
		}
		
		.icon {
			position: absolute;
			left: 0;
		}
	}
	.custom-style {
		background-color: #FF6234 !important;
	}
</style>
